/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.Ilusio.messaging;

import edu.upf.Ilusio.Entities.Picture;
import edu.upf.Ilusio.Interfaces.Listener;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.muc.MultiUserChat;

/**
 *
 * @author Madhava Carrillo <madhava at me.com>
 */
public class IM {

    private XMPPConnection con = null;
    private MultiUserChat chat = null;
    private JTextArea ta;

    public IM(JTextArea ta) {
        this.ta = ta;
    }

    public void logout() {
        if (con != null) {
            con.disconnect();
        }
    }

    public void Send(String rx, String msgstr) {
        Roster roster = con.getRoster();
        Presence p = roster.getPresence(rx);
        //System.out.println(rx + "-" + msgstr);
        Message msg = new Message();
        msg.setPacketID(Config.PacketChatid);
        msg.setTo(rx);
        msg.setBody(msgstr);
        msg.setProperty("fecha", new Date());
        try {
            chat.sendMessage(msg);
        } catch (XMPPException ex) {
            Logger.getLogger(IM.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

    public void SendSearchProfile(String rx, Picture searchPic ){
        Roster roster = con.getRoster();
        Presence p = roster.getPresence(rx);
        //System.out.println(rx + "-" + msgstr);
        Message msg = new Message();
        msg.setPacketID(Config.PacketSearchProfileid);
        msg.setTo(rx);
        msg.setProperty("searchPic", searchPic);
        try {
            chat.sendMessage(msg);
        } catch (XMPPException ex) {
            Logger.getLogger(IM.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

    public boolean login(String aLogin, final String aPassword, String resource) {
        boolean result;
        try {
            ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
            config.setSASLAuthenticationEnabled(true);
            config.setReconnectionAllowed(true);
            con = new XMPPConnection(config);
            con.connect();
            con.login(aLogin, aPassword, resource);
            chat = new MultiUserChat(con, aLogin + "'s Chat");
            System.out.println(con.getUser());
            result = true;
        } catch (Throwable ex) {
            ex.printStackTrace();
            result = false;
        }
        return result;
    }

    public void addText(String rx, String msg) {
        Roster roster = con.getRoster();
        Presence p = roster.getPresence(rx);
        if (p.getStatus().equalsIgnoreCase(Config.presence)) {
            ta.append(rx + " - " + msg+"\n");
        }
    }

    public void changePresence(String presencestr) {
        //Cambiar mi status
        Presence presence = new Presence(Presence.Type.available);
        presence.setStatus(presencestr);
        con.sendPacket(presence);
    }

    public void addListener(Listener list, PacketFilter filter) {
        con.createPacketCollector(filter);
        con.addPacketListener(list, filter);
    }
}
